package job.relevance.sort;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RLV_Sort {

    private static final String filename = "/home/renp/a3/Text.res.pagerank";

    private static final int BLOCK_LINE = 14;
    
    private static List<String> list = new ArrayList<String>();

    private static Map<String, List<String>> map = new HashMap<String, List<String>>();

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(filename));
        int lineNo = 0;
        String k = "";
        for (String line = br.readLine(); line != null; line = br.readLine()) {
            if (lineNo % BLOCK_LINE == 0) {
                k = line;
                list.add(k);
                map.put(k, new ArrayList<String>());
            }
            lineNo++;
            map.get(k).add(line);
        }
        br.close();
        
        Collections.sort(list);
        BufferedWriter bw = new BufferedWriter(new FileWriter(filename + ".sorted"));
        for (String s : list) {
            for (String t : map.get(s)) {
                bw.write(t);
                bw.newLine();
            }
        }
        bw.flush();
        bw.close();
    }

}
